package controller

import (
	"apitestAfterGo/common"
	"apitestAfterGo/domain"
	"apitestAfterGo/dto"
	"apitestAfterGo/service"
	"apitestAfterGo/utils"
	"github.com/kataras/iris/v12/context"
	"github.com/kataras/iris/v12/mvc"
)

type SystemController struct {
}

func (c *SystemController) BeforeActivation(b mvc.BeforeActivation) {
	// 1 ->  HTTP 方法
	// 2 -> 路由的路径
	// 3 -> 此控制器的方法名称，该名称应为此路由的处理程序。
	b.Handle("POST", "/getSystems", "GetSystems")
	b.Handle("POST", "/getSystemsByPage", "GetSystemsByPage")
	b.Handle("DELETE", "/deleteSystemById/{id:int}", "DeleteSystemById")
	b.Handle("POST", "/addSystem", "AddSystem")
	b.Handle("POST", "/modifySystemById", "ModifySystemById")
	b.Handle("GET", "/getUsersBySystemId/{id:int}", "GetUsersBySystemId")
	b.Handle("GET", "/getSystemById/{id:int}", "GetSystemById")
	b.Handle("POST", "/modifyUsersBySystemId", "ModifyUsersBySystemId")
}

func (c *SystemController) GetSystems(context *context.Context, dto dto.SystemByPageInputDto) common.Result {
	dto.UserId = int(utils.GetJwtParameter(context, "id").(float64))
	count, _ := service.SystemServiceImpl.GetAllSystemCount(dto)
	pages := utils.GetPages(count, dto.PageSize)
	systems, _ := service.SystemServiceImpl.GetSystems(dto)
	page := domain.Page{
		Pages:   pages,
		Total:   count,
		Current: dto.Current,
		Size:    dto.PageSize,
		Records: systems,
	}
	return utils.Success(page, "获取用户系统成功！")
}
func (c *SystemController) GetSystemsByPage(dto dto.SystemByPageInputDto) common.Result {
	count, _ := service.SystemServiceImpl.GetAllSystemCount(dto)
	pages := utils.GetPages(count, dto.PageSize)
	systems, _ := service.SystemServiceImpl.GetSystemsByPage(dto)
	page := domain.Page{
		Pages:   pages,
		Total:   count,
		Current: dto.Current,
		Size:    dto.PageSize,
		Records: systems,
	}
	return utils.Success(page, "获取系统成功！")
}

func (c *SystemController) DeleteSystemById(id int) common.Result {
	result, _ := service.SystemServiceImpl.DeleteSystemById(id)
	return utils.Success(result, "删除系统成功！")
}

func (c *SystemController) AddSystem(dto dto.SystemInputDto) common.Result {
	result, _ := service.SystemServiceImpl.AddSystem(dto)
	return utils.Success(result, "添加系统成功！")
}

func (c *SystemController) ModifySystemById(context *context.Context, dto dto.SystemInputDto) common.Result {
	username := utils.GetJwtParameter(context, "username").(string)
	dto.UpdateBy = username
	result, _ := service.SystemServiceImpl.ModifySystemById(dto)
	return utils.Success(result, "更改系统成功！")
}

func (c *SystemController) GetUsersBySystemId(id int) common.Result {
	result, _ := service.SystemServiceImpl.GetUsersBySystemId(id)
	return utils.Success(result, "获取用户id成功！")
}

func (c *SystemController) GetSystemById(id int) common.Result {
	result, _ := service.SystemServiceImpl.GetSystemById(id)
	return utils.Success(result, "获取用户系统成功！")
}
func (c *SystemController) ModifyUsersBySystemId(dto dto.GrantSystemInputDto) common.Result {
	result, _ := service.SystemServiceImpl.AddUsersBySystemId(dto)
	return utils.Success(result, "系统分配用户成功！")
}
